<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Zabbix监控字符集问题 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Zabbix监控字符集问题</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix监控中常碰到的字符集问题有三个：</p>
<h2 id="一、图形中显示中文乱码"><a href="#一、图形中显示中文乱码" class="headerlink" title="一、图形中显示中文乱码"></a>一、图形中显示中文乱码</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方法：</p>
<ol>
<li><p>从window主机路径C:\Windows\Fonts下选择一种自己喜欢的字体，如mysh.ttf(雅黑)；</p>
</li>
<li><p>将拷贝出来的字体上传到zabbix server网页文件的fonts目录下，并命名为<strong>DejaVuSans.ttf</strong></p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mv /var/www/html/fonts/DejaVuSans.ttf /var/www/html/fonts/DejaVuSans.ttf.old</span></div><div class="line">mv /var/www/html/fonts/mysh.ttf /var/www/html/fonts/DejaVuSans.ttf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样便能解决这个乱码问题</p>
<h2 id="二、历史记录处出现问号乱码"><a href="#二、历史记录处出现问号乱码" class="headerlink" title="二、历史记录处出现问号乱码"></a>二、历史记录处出现问号乱码</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这与zabbix数据库所使用的字符集有关，解决方法如下：</p>
<ol>
<li>查看当前的默认字符集</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create database zabbix;</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                   |</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET latin1 */ |</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div><div class="line"><span class="comment">#安装时在导入三个sql文件时，zabbix数据库中创建的表的字符集也是latin1</span></div><div class="line">mysql&gt; show variables like <span class="string">'character%'</span>;</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| Variable_name            | Value                      |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| character_set_client     | latin1                     |</div><div class="line">| character_set_connection | latin1                     |</div><div class="line">| character_set_database   | latin1                     |</div><div class="line">| character_set_filesystem | binary                     |</div><div class="line">| character_set_results    | latin1                     |</div><div class="line">| character_set_server     | latin1                     |</div><div class="line">| character_set_system     | utf8                       |</div><div class="line">| character_sets_dir       | /usr/share/mysql/charsets/ |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ol>
<li>这是由于字符集不是utf8引起的，现在的解决方法有两种：</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当当前zabbix数据库中已存在一定的数据的解决办法</p>
<ul>
<li>备份zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mysqldump -uroot -p123456 zabbix &gt; zabbix.sql</span></div></pre></td></tr></table></figure>
<ul>
<li>修改备份文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># sed -i 's/latin1/utf8/g' zabbix.sql</span></div></pre></td></tr></table></figure>
<ul>
<li>删除zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop database zabbix;</div></pre></td></tr></table></figure>
<ul>
<li>关闭mysql数据库，设置默认字符集</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># vim /etc/my.cnf</span></div><div class="line">[mysqld]</div><div class="line"><span class="built_in">log</span>-bin</div><div class="line">datadir=/var/lib/mysql</div><div class="line">socket=/var/lib/mysql/mysql.sock</div><div class="line">default-character-set = utf8 <span class="comment">#添加该项</span></div></pre></td></tr></table></figure>
<ul>
<li>启动mysql并恢复zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mysql -uroot -p123456 zabbix &lt; zabbix.sql</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create database zabbix;</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                 |</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 */ |</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div><div class="line">mysql&gt; show variables like <span class="string">'character%'</span>;</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| Variable_name            | Value                      |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| character_set_client     | latin1                     |</div><div class="line">| character_set_connection | latin1                     |</div><div class="line">| character_set_database   | utf8                       |</div><div class="line">| character_set_filesystem | binary                     |</div><div class="line">| character_set_results    | latin1                     |</div><div class="line">| character_set_server     | utf8                       |</div><div class="line">| character_set_system     | utf8                       |</div><div class="line">| character_sets_dir       | /usr/share/mysql/charsets/ |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ul>
<li>访问页面</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/03.png?raw=true" alt=""></figure></p>
<h2 id="当安装后发现，则可以："><a href="#当安装后发现，则可以：" class="headerlink" title="当安装后发现，则可以："></a>当安装后发现，则可以：</h2><ol>
<li>删除zabbix数据库</li>
<li>设置mysql数据库的默认字符集为utf8</li>
<li>重新创建zabbix数据库并导入三个sql文件</li>
</ol>
<h2 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">--with-charset=CHARSET</div><div class="line">Default character <span class="built_in">set</span>, use one of:</div><div class="line">binary</div><div class="line">armscii8 ascii big5 cp1250 cp1251 cp1256 cp1257</div><div class="line">cp850 cp852 cp866 cp932 dec8 eucjpms euckr gb2312 gbk geostd8</div><div class="line">greek hebrew hp8 keybcs2 koi8r koi8u</div><div class="line">latin1 latin2 latin5 latin7 macce macroman</div><div class="line">sjis swe7 tis620 ucs2 ujis utf8</div><div class="line">--with-extra-charsets=CHARSET,CHARSET,...</div><div class="line">Use charsets <span class="keyword">in</span> addition to default (none, complex,</div><div class="line">all, or a list selected from the above sets)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这两个是编译安装mysql事对字符集设置的参数，当不进行设置时默认便是latin1，</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/10. Zabbix监控字符集问题/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
